<template>
  <div class="info_chang_box">
    <swiper class="swiper" :options="swiperOption" v-if="bannerList.length>0">
      <swiper-slide v-for="(item,index) in bannerList" :key="index">
        <img :src="item.image">
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
    <div class="left bout" slot="prevEl"></div>
    <div class="right bout" slot="nextEl"></div>
  </div>
</template>

<script>
import 'swiper/dist/css/swiper.css'
import {swiper, swiperSlide} from 'vue-awesome-swiper'

export default {
  name: "banner",
  components: {
    swiper,
    swiperSlide
  },
  props: {
    bannerList: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    const _this = this
    return {
      swiperOption: {
        autoplay: true,
        loop: true,
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.right',
          prevEl: '.left'
        },
        on: {
          click: function () {
            // 这里有坑，需要注意的是：this 指向的是 swpier 实例，而不是当前的 vue， 因此借助 vm，来调用 methods 里的方法
            // 当前活动块的索引，与activeIndex不同的是，在loop模式下不会将 复制的块 的数量计算在内。
            _this.handleClickSlide(this);
          }
        }
      }
    }
  },
  mounted() {
  },
  methods:{
    handleClickSlide(vm) {
      let index = vm.clickedIndex - vm.activeIndex + vm.realIndex === this.bannerList.length ? 0 : vm.clickedIndex - vm.activeIndex + vm.realIndex;
      // 我得到的index就是点击的item在实际数组中的下标index
      // 后面可以根据得到的index获取到ID跳到每个对应的路由;
      if(this.bannerList[index].is_jump){
        window.open(this.bannerList[index].jump_link)
      }
      // window.open(this.bannerList[index].link)
    },
  }
}
</script>

<style scoped lang="less">
.info_chang_box {
  position: relative;
  height: 464px;
  cursor: pointer;
  .bout {
    width: 42px;
    height: 42px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 5;
    border-radius: 50%;
  }

  .right {
    right: 120px;
    background-image: url("../assets/banner-right.png");
    background-size: contain;
  }

  .left {
    left: 120px;
    background-image: url("../assets/banner-left.png");
    background-size: contain;
  }
}
.swiper {
  position: relative;
  height: 100%;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .swiper-pagination {
    bottom: 21px;
    /deep/ .swiper-pagination-bullet {
      width: 32px;
      height: 4px;
      background: #FFFFFF;
      opacity: 1;
      border-radius: 0;
      margin-right: 24px;
    }
    /deep/ .swiper-pagination-bullet-active {
      background: #B11E24;
    }
  }
}


</style>